﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorkStation.Frameworks.EasyBus.MessageQueue;

namespace WorkStation.Frameworks.EasyBus.MessageQueue.RabbitMq
{
    // RabbitMq:HostName={Host IP};VirtualHost={@"/"};UserName={User Name};Password={Password};Port={Port}

    /// <summary>
    /// RabbitMq 地址翻译器实现
    /// </summary>
    public class RabbitMqAddressTranslator : QueueAddressTranslatorBase
    {
        #region 私有静态变量

        protected static readonly string HOST_NAME_KEY = "HostName";
        protected static readonly string VIRTUAL_HOST_KEY = "VirtualHost";
        protected static readonly string USER_NAME_KEY = "UserName";
        protected static readonly string PASSWORD_KEY = "Password";
        protected static readonly string PORT_KEY = "Port";
        protected static readonly string QUEUE_NAME_KEY = "QueueName";

        #endregion

        #region 公有属性

        public override string Prefix
        {
            get { return "RabbitMq"; }
        }

        public override Type EndpointType { get; protected set; }

        #endregion

        public RabbitMqAddressTranslator() 
        {
            this.ParameterNames.Add(HOST_NAME_KEY);
            this.ParameterNames.Add(VIRTUAL_HOST_KEY);
            this.ParameterNames.Add(USER_NAME_KEY);
            this.ParameterNames.Add(PASSWORD_KEY);
            this.ParameterNames.Add(QUEUE_NAME_KEY);
            this.ParameterNames.Add(PORT_KEY);

            this.EndpointType = typeof(RabbitMqEndpoint);
        }

        protected override IEndpoint TranslateToEndpointCore(Dictionary<string, string> parameters)
        {
            return new RabbitMqEndpoint() 
            { 
                HostName = parameters[HOST_NAME_KEY],
                VirtualHost = parameters[VIRTUAL_HOST_KEY],
                Password = parameters[PASSWORD_KEY],
                UserName = parameters[USER_NAME_KEY],
                Port = Convert.ToInt32(parameters[PORT_KEY]),
                QueueName = parameters[QUEUE_NAME_KEY]
            };
        }
    }
}
